{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wavenet Demo\n",
    "Demo of our efficient generation implementation.\n",
    "\n",
    "Trains wavenet on a single wav file. Then generates that file, starting from a single sample."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from time import time\n",
    "\n",
    "from wavenet.utils import make_batch\n",
    "from wavenet.models import Model, Generator\n",
    "\n",
    "from IPython.display import Audio\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "inputs, targets = make_batch('assets/voice.wav')\n",
    "num_time_samples = inputs.shape[1]\n",
    "num_channels = 1\n",
    "gpu_fraction = 1.0\n",
    "\n",
    "model = Model(num_time_samples=num_time_samples,\n",
    "              num_channels=num_channels,\n",
    "              gpu_fraction=gpu_fraction)\n",
    "\n",
    "Audio(inputs.reshape(inputs.shape[1]), rate=44100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tic = time()\n",
    "model.train(inputs, targets)\n",
    "toc = time()\n",
    "\n",
    "print('Training took {} seconds.'.format(toc-tic))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "generator = Generator(model)\n",
    "\n",
    "# Get first sample of input\n",
    "input_ = inputs[:, 0:1, 0]\n",
    "\n",
    "tic = time()\n",
    "predictions = generator.run(input_, 32000)\n",
    "toc = time()\n",
    "print('Generating took {} seconds.'.format(toc-tic))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Audio(predictions, rate=44100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
